/*
 * httpConnections.cpp
 *
 *  Created on: Apr 24, 2011
 *      Author: xrose
 */

#include "httpConnections.h"

string httpConnections::createGetRequest(){
	string result("GET /"+_url.getIdentifier()+" HTTP/1.1\r\nHost: "+_url.getHostname()+"\r\n\r\n");
	return result;
}
string httpConnections::createGetRangeRequest(int stByte, int endByte){
	string str_stByte = Util::convertIntToString(stByte);
	string str_endByte = Util::convertIntToString(endByte);
	string result("GET /"+_url.getIdentifier()+" HTTP/1.1\r\n"+
			"Host: "+_url.getHostname()+"\r\n"+
			"Range: bytes="+str_stByte+"-"+str_endByte+"\r\n\r\n");
	return result;
}
string httpConnections::createHeadRequest(){
	string result("HEAD /" + _url.getIdentifier()+" HTTP/1.1\r\nHost: "+_url.getHostname()+"\r\n\r\n");
	return result;
}

bool httpConnections::sendGet(){
	string request = createGetRequest();
	if(!(_socket.create())) return false;
	if(!(_socket.connect(_url.getHostname(), _url.getPort()))) return false;
	return _socket.send(request);
}
bool httpConnections::sendGetRange(ulong stBye, ulong endByte){
	cout<<"DEBUG: ...httpConnections::senGetRange(): stByte= "<<stBye<<" -+- endByte = "<<endByte<<endl;
	string request = createGetRangeRequest(stBye, endByte);
	if(!(_socket.create())) return false;
	if(!(_socket.connect(_url.getHostname(), _url.getPort()))) return false;
	cout<<"DEBUG: ___httpConnections::senGetRange():request = " <<request<<endl;
	return _socket.send(request);
}
string httpConnections::getHead(){
	cout<<"...getHead()"<<endl;
	char buffer[BUFFER_SIZE];
	string request = createHeadRequest();
	cout<<"DEBUG: +getHead()"<<endl;
	cout<<"DEBUG: headRequest = "<<request<<endl;

	if(!(_socket.create())) return NULL;
	cout<<"++getHead()"<<endl;
	if(!(_socket.connect(_url.getHostname(), _url.getPort()))) return NULL;
	cout<<"+++getHead()"<<endl;
	if(!(_socket.send(request))) return NULL;
	if(_socket.receive(buffer)<=0) return NULL;
	cout<<"___getHead()"<<endl;
	return string(buffer);
}
bool httpConnections::close(){
	return _socket.close();
}
int httpConnections::receiveData(thrdStruct &infoThread){
	cout<<"DEBUG:...httpConnections::receiveData()"<<endl;
	bool isFirst = true;
	if(infoThread.getDlStatus() == 1) return 0;	//buffer has not been read
	int readByte = _socket.receive(infoThread.buffer);
	cout<<"DEBUG:...httpConnections::receiveData()::readByte = "<<readByte<<endl;
	if(readByte<=0) return readByte;
	//success! then strip out the header
	if(infoThread.getDledByte() == 0){
		char tmpBuffer[BUFFER_SIZE];
		memset(tmpBuffer, 0x0, BUFFER_SIZE);
		memcpy(tmpBuffer, infoThread.buffer, readByte);
		char* file_pos = (char*)strstr(tmpBuffer, "\r\n\r\n");
		file_pos+=4;
		long cursor = file_pos - tmpBuffer;
		isFirst = false;
		//--
		memset(infoThread.buffer, 0x0, BUFFER_SIZE);
		readByte -= cursor;
		cout<<"DEBUG: ---httpConnections::receiveData()::readByte = "<<readByte<<endl;
		memcpy(infoThread.buffer, file_pos, readByte);
	}
	infoThread.setDledByte(infoThread.getDledByte() + readByte); //update doawloadedByte
	infoThread.setDlStatus(readByte);	//buffer full
	cout<<"DEBUG:__httpConnections::receiveData()"<<endl;
	return readByte;
}
